package com.firewolf.dubbo.mains;

import java.util.concurrent.Future;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.rpc.RpcContext;
import com.firewolf.s07.service.AsyncService;

public class AsyncConsumer {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {

		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				new String[] { "consumer.xml" });
		AsyncService bean = ctx.getBean(AsyncService.class);
		long start = System.currentTimeMillis();
		//此时获取到的值不是正确的结果
		int a = bean.method1();
		Future<Integer> aFuture = RpcContext.getContext().getFuture(); // 拿到调用的Future引用，当结果返回后，会被通知和设置到此Future
		System.out.println("a=" + a);
		int b = bean.method2();
		Future<Integer> bFuture = RpcContext.getContext().getFuture(); 
		System.out.println("b=" + b);
		//等待获取正确的结果值
		Integer a2 = aFuture.get();
		Integer b2 = bFuture.get();
		System.out.println(a2+","+b2);
		long end = System.currentTimeMillis();
		System.out.println("一共耗时" + (end - start) / 1000 + "秒");
	}
}
